package com.hc.comm.Service;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.hc.bean.RoleBean;
import com.hc.bean.UrlBean;
import com.hc.comm.CommData;
import com.hc.dao.RunSqlDao;
import com.hc.daoImpl.RunSqlDaoImpl;
import com.hc.radius.service.Order;
import com.hc.util.BSizeConfigUtil;
import com.hc.util.SystemConfigUtil;

public class CommDataService  implements ServletContextListener{

	public  void loadUrlData(){
		
		RunSqlDao dao = new RunSqlDaoImpl();
		
		List list = dao.runSqlQuery("select indexe ,url from urlList");
		
		for(Object obj:list){
			
			Map map = (Map) obj;
		
			CommData.urlMap.put(map.get("url").toString(),Integer.parseInt( map.get("indexe").toString()));
			
			
			
		}
	}
	
	public void getUserUrl(){
		RunSqlDao dao = new RunSqlDaoImpl();
		List list = dao.runSqlQuery("select id, testTimes, testUrl from userUrlList");
		
		for(Object obj : list){
			Map map = (Map) obj;
			UrlBean bean = new UrlBean();
			bean.setId(Integer.valueOf(map.get("id").toString()));
			bean.setTestTimes(Integer.valueOf(map.get("testTimes").toString()));
			bean.setTestUrl(map.get("testUrl").toString());
			CommData.urls.put(bean.getId(), bean);
		}
	}

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
	
		
	}

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		  CommData.systemPath =sce.getServletContext().getRealPath("/");  
		  //创建文件夹
		  mkdirsFolder();
			System.out.println("先加系统配置文件");
		  System.out.println( CommData.systemPath);

		SystemConfigUtil.readConfig();
		
		//获取当前所在路径
		
		
//		//加载urlList
//		loadUrlData();
		getUserUrl();
		
		readGradeConfig();
		
		
		RoleBean bean = new RoleBean();
		bean.setAddress("成都庆安社区");
		Order o = new Order();
		o.setName("4M");
		o.setBroadbandSize("false");
		bean.setOrder(o);
		bean.setCompany("高新区分公司");
		bean.setExternalIp("3689901706");
		bean.setIntranetIp("3689901706");
		bean.setNumber("testNumber");
		bean.setVlanId1("1563");
		bean.setVlanId2("1234");
		CommData.roles.put("testNumber", bean);
		
		
		
		
		
	}

	private void mkdirsFolder() {
		  File file = new File(CommData.excelTempFilePath);
		  
			if(!file.exists()){
				file.mkdirs();
			}
		
	}

	private void readGradeConfig() {
		 CommData.BSizeBeans = BSizeConfigUtil.readConfig();
		
	}
	

}
